﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace katOS
{
    /// <summary>
    /// Class that has the system programs
    /// </summary>
    class SystemPrograms
    {
        /// <summary>
        /// Method ls to show the files on the OS folder
        /// </summary>
        public void ls()
        {
            DirectoryInfo root = new DirectoryInfo("C:/Users/kareAndy/Documents/UVG/Ciclo 5/CC3002 Sistemas Operativos/Proyecto No. 1/katOSFiles");
            FileInfo[] listFiles = root.GetFiles("*.*");
            foreach (FileInfo a_file in listFiles)
            {
                Console.WriteLine(a_file.Name);
            }
            Console.WriteLine();
        }

        /// <summary>
        /// Method to kill a process
        /// </summary>
        /// <param name="pid">Recieves the process ID from that belongs to the process to kill</param>
        public void kill(String pid)
        {
            int pid_to_kill=-1;
            try{
                pid_to_kill = int.Parse(pid);
               
                for(int i=0 ; i<Scheduler.queue_list.Count; i++)
                {
                    for (int j = 0; j < Scheduler.queue_list.ElementAt(i).getQueue().Count; j++)
                    {
                        if (Scheduler.queue_list.ElementAt(i).getQueue().ElementAt(j).getPID()== pid_to_kill)
                        {
                            Scheduler.process_to_kill.AddLast(Scheduler.queue_list.ElementAt(i).getQueue().ElementAt(j));
                            break;
                        }
                    }
                }

            }catch(Exception){
                Console.WriteLine("Wrong kill parameters"); ;
            }
            Console.WriteLine("Killing process");
        }
        
        /// <summary>
        /// Method to show the process that are excecuting on the OS
        /// </summary>
        public void ps()
        {
            String output = "";

            LinkedList<Process>temp_queue = Scheduler.all_processes;
            if (temp_queue.Count <= 0)
            {
                output += "No process on the queue";
            }

            for (int i = 0; i < temp_queue.Count; i++)
            {
                output += "Process: " + temp_queue.ElementAt(i).getPID() + ", State: " + temp_queue.ElementAt(i).getState() + "\n";
                output += "\t" + "Priority: " + temp_queue.ElementAt(i).priority + "\n";
                output += "\t" + "Is on queue numbre: " + temp_queue.ElementAt(i).queue + "\n";
            }
            Console.WriteLine(output);
        }
    }
}
